package com.qqtech.qquan.mall.dao;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import com.qqtech.core.common.util.BeanUtil;
import com.qqtech.core.frame.dao.BaseDaoImpl;
import com.qqtech.qquan.mall.model.MallProduct;
import com.qqtech.qquan.mall.model.MallProductQuery;
import com.qqtech.qquan.mall.model.MallProductVo;

@Repository
public class MallProductDaoImpl extends BaseDaoImpl<MallProduct> implements MallProductDao {
	public static final Logger log = LoggerFactory.getLogger(MallProductDaoImpl.class);

	@Override
	public Page<MallProductVo> queryPageListAdmin(MallProduct query,
			Pageable pageable) {
		List<MallProductVo> contentList = sqlSessionTemplate.selectList(getSqlName("selectAdmin"), getParams(query, pageable));
		return new PageImpl<MallProductVo>(contentList, pageable, this.queryCountAdmin(query));
	}

	@Override
	public int queryCountAdmin(MallProduct query) {
		return sqlSessionTemplate.selectOne(getSqlName("selectCountAdmin"), BeanUtil.toMap(query));
	}

	@Override
	public int reduceStock(int productId, int quantity) {
		MallProduct product = new MallProduct();
        product.setId(productId);
        product.setStock(quantity);
        return sqlSessionTemplate.update(getSqlName("reduceStock"), product);
	}

	@Override
	public int addStock(int productId, int quantity) {
		MallProduct product = new MallProduct();
        product.setId(productId);
        product.setStock(quantity);
        return sqlSessionTemplate.update(getSqlName("addStock"), product);
	}

	@Override
	public MallProduct getProductBySpecId(Integer specId) {
		return sqlSessionTemplate.selectOne(getSqlName("getProductBySpecId"), specId);
	}

	@Override
	public int updateFeedbackCount(Integer productId) {
		return sqlSessionTemplate.update(getSqlName("updateFeedbackCount"), productId);
	}

	@Override
	public Page<MallProductVo> queryPageBySubject(MallProductQuery subject,
			Pageable pageable) {
		List<MallProductVo> contentList = sqlSessionTemplate.selectList(getSqlName("queryPageBySubject"), getParams(subject, pageable));
		return new PageImpl<MallProductVo>(contentList, pageable, this.queryCountBySubject(subject));
	}

	@Override
	public int queryCountBySubject(MallProductQuery query) {
		return sqlSessionTemplate.selectOne(getSqlName("queryCountBySubject"), BeanUtil.toMap(query));
	}

	@Override
	public Page<MallProductVo> queryPageByShop(MallProductQuery query,
			Pageable pageable) {
		List<MallProductVo> contentList = sqlSessionTemplate.selectList(getSqlName("queryPageByShop"), getParams(query, pageable));
		return new PageImpl<MallProductVo>(contentList, pageable, this.queryCountByShop(query));
	}

	@Override
	public int queryCountByShop(MallProductQuery query) {
		return sqlSessionTemplate.selectOne(getSqlName("queryCountByShop"), BeanUtil.toMap(query));
	}

	@Override
	public List<MallProductVo> queryShopSellCategory(Integer shopId) {
		if (shopId == null) {
			return null;
		}
		List<MallProductVo> contentList = sqlSessionTemplate.selectList(getSqlName("queryShopSellCategory"), shopId);
		return contentList;
	}

	@Override
	public int increasePv(int productId, int pv) {
		MallProduct product = new MallProduct();
		product.setId(productId);
		product.setPv(pv);
		return sqlSessionTemplate.update(getSqlName("increasePv"), product);
	}

	@Override
	public int increaseConcern(Integer productId, int addCount) {
		MallProduct product = new MallProduct();
		product.setId(productId);
		product.setConcernCount(addCount);		
		return sqlSessionTemplate.update(getSqlName("increaseConcern"), product);
	}
	
}
